package com.sai.open;
import android.app.Activity;
import android.os.Bundle;
import java.util.List;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import android.widget.Toast;
import android.widget.TextView;
import com.sai.open.sky.R;
import com.sai.tool.FontManager;
import android.graphics.Bitmap;
import android.content.res.AssetManager;
import java.io.InputStream;
import android.graphics.BitmapFactory;
import android.content.Context;
import java.io.IOException;
import android.app.ProgressDialog;

public class BaseActivity extends Activity
{
	public FontManager fontManager;
	public ProgressDialog waitingDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		fontManager = new FontManager(this, "fonts/ipix.ttf");
	}
	
	public void showWaitingDialog(String title, String info)
	{
		/* 等待Dialog具有屏蔽其他控件的交互能力
		 * @setCancelable 为使屏幕不可点击，设置为不可取消(false)
		 * 下载等事件完成后，主动调用函数关闭该Dialog
		 */
	    waitingDialog = new ProgressDialog(this);
		waitingDialog.setTitle(title);
		waitingDialog.setMessage(info);
		waitingDialog.setIndeterminate(true);
		waitingDialog.setCancelable(false);
		waitingDialog.show();
	}

	public void toast(String str)
	{
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
	}

	public void setGameTitle(String title){
		TextView tv = findViewById(R.id.game_title_text);
		tv.setText(title);
	}

	public String getGameTitle(){
		TextView tv = findViewById(R.id.game_title_text);
		return tv.getText().toString();
	}
	
	public void changeFont()
	{
		fontManager.changeFonts(getRootView());
	}

	public void changeFont(ViewGroup view)
	{
		fontManager.changeFonts(view);
	}

	public ViewGroup getRootView()  
    {  
        return this.findViewById(android.R.id.content);  
    }

	//-----------------获取 activity中的所有view
    public List<View> getAllViews()
	{
        List<View> list = getAllChildViews(getRootView());
		return list;
    }

    public List<View> getAllChildViews(View view)
	{
        List<View> allchildren = new ArrayList<View>();
        if (view instanceof ViewGroup)
		{
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++)
			{
                View viewchild = vp.getChildAt(i);
                allchildren.add(viewchild);
                //再次 调用本身（递归）
                allchildren.addAll(getAllChildViews(viewchild));
            }
        }
        return allchildren;
    }
	
	/**
	 * 从Assets中读取图片
	 */
	public static Bitmap getImageFromAssetsFile(String fileName, Context context) {
		Bitmap image = null;
		AssetManager am = context.getResources().getAssets();
		try {
			InputStream is = am.open(fileName);
			image = BitmapFactory.decodeStream(is);
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return image;
	}
}
